package com.ruoyi.web.controller.communication;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Communication;
import com.ruoyi.system.service.ICommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通讯录控制层
 * 
 * @author 丁
 */
@RestController
@RequestMapping("/communication")
public class CommunicationController extends BaseController {
    @Autowired
    private ICommunicationService communicationService;

    /**
     * 新增保存通讯信息
     */
    @Log(title = "通讯录新增", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:communication:export')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Communication communication) {
        //把创建者修改成当前用户
        communication.setCreateBy(getUsername());

        //添加通讯录，并返回通一响应结果
        return toAjax(communicationService.insert(communication));
    }

    /**
     * 删除通讯信息
     */
    @PreAuthorize("@ss.hasPermi('system:communication:remove')")
    @Log(title = "删除通讯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(communicationService.delete(Ids));
    }

    /**
     * 修改通讯信息
     */
    @PreAuthorize("@ss.hasPermi('system:communication:edit')")
    @Log(title = "通讯信息修改", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Communication communication)
    {
        //把创建者修改成当前用户
        communication.setUpdateBy(getUsername());
        return toAjax(communicationService.update(communication));
    }

    /**
     * 查询所有通讯信息
     */
    @PreAuthorize("@ss.hasPermi('system:communication:list')")
    @GetMapping("/list")
    public TableDataInfo list(Communication communication)
    {
        startPage();
        startOrderBy();
        List<Communication> list = communicationService.select(communication);
        return getDataTable(list);
    }

    /**
     * 根据ID获取通讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:getId')")
    @GetMapping(value = "/{id}")
    public AjaxResult getId(@PathVariable Long id)
    {
        return success(communicationService.selectId(id));
    }

}
